﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PoshBoard.VisualDesigner.Controls;
using System.Reflection;

namespace PoshBoard.VisualDesigner.Designer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Closed += new EventHandler(MainWindow_Closed);

            //Using reflexion in order to get All Window available in the project
            List<DesignWindow> designs = new List<DesignWindow>();
            Assembly MyDALL = Assembly.GetExecutingAssembly();
            foreach (Type type in MyDALL.GetTypes())
            {
                if (type.BaseType.Name == "Window" && type.Name !=  "MainWindow")
                {
                    designs.Add(new DesignWindow() { Name = type.Name, WindowType = type });
                }
            }
            this.comboSelectDesign.ItemsSource = designs;
        }

        void MainWindow_Closed(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void btn_Copy_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetDataObject(this.textBox1.Text, true);
        }



        public class DesignWindow
        {
            public string Name { get; set; }
            public Type WindowType { get; set; }
        }

        private void comboSelectDesign_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            Type selectype = ((DesignWindow)this.comboSelectDesign.SelectedItem).WindowType;

            //create an instance of the selected type
            Window instance = (Window)Activator.CreateInstance(selectype);

            Grid grid = instance.Content as Grid;

            // "convert" of the Grid un NewGrid type
            NewGrid mainGrid = new NewGrid();
            if (grid.Name == string.Empty)
                mainGrid.Name = "Grid1";
            else
                mainGrid.Name = grid.Name;

            int iMax = grid.ColumnDefinitions.Count;
            for (int i = 0; i < iMax; i++)
            {
                ColumnDefinition def = grid.ColumnDefinitions[0];
                grid.ColumnDefinitions.Remove(def);
                mainGrid.ColumnDefinitions.Add(def);
            }

            iMax = grid.RowDefinitions.Count;
            for (int i = 0; i < iMax; i++)
            {
                RowDefinition def = grid.RowDefinitions[0];
                grid.RowDefinitions.Remove(def);
                mainGrid.RowDefinitions.Add(def);
            }

            iMax = grid.Children.Count;
            for (int i = 0; i < iMax; i++)
            {
                UIElement control = grid.Children[0];
                grid.Children.Remove(control);
                mainGrid.Children.Add(control);
            }

            mainGrid.BuildPSName(string.Empty);

            this.textBox1.Text = mainGrid.GetConstructionScript();

        }
    }
}
